<?php 
namespace Member\Controller;

class ReplyController extends ManageController {
	//自定义回复设置
	function index($appid) {
		$map['appid'] = $appid;
		$list = $this->lists('Reply', $map);
		$this->assign('__LIST__', $list);
		set_redirect_url(__SELF__);
		$this->title('自定义回复');
		$this->display();
	}
	
	function undefine() {
		if (IS_POST) {
			$model = D('ReplyUndefine');
			if ($model->update()) {
				$this->success('保存成功！');
			}else {
				$this->error('保存失败！');
			}
		}else {
			$map['uid'] = UID;
			$map['appid'] = I('get.appid');
			$info = M('ReplyUndefine')->where($map)->find();
			$this->assign('info', $info);
			$this->title('无应答回复');
			$this->display();
		}
	}
	
	//关注时回复设置
	function attention($appid) {
		if (IS_POST) {
			$model = D('ReplyAttention');
			if ($model->update()) {
				$this->success('保存成功！');
			}else {
				$this->error('保存失败！');
			}
		}else {
			$map['uid'] = UID;
			$map['appid'] = I('get.appid');
			$info = M('ReplyAttention')->where($map)->find();
			$this->assign('info', $info);
			$this->title('关注时回复');
			$this->display();
		}
	}
	
	//添加自定义回复
	function add($appid) {
		$this->title('添加自定义回复');
		$this->display('edit');
	}
	
	//编辑自定义回复
	function edit($id, $appid) {
		$model = D('Reply');
		$info  = $model->detail($id);
		$this->assign('info', $info);
		$this->title('编辑自定义回复');
		$this->display();
	}
	
	//删除自定义回复
	function del($id, $appid) {
		$model = M('Reply');
		$info = $this->oFind($model, $id);
		if ($model->delete($id)) {
			//删除扩展
			M('Reply_'.$info['type'])->delete($id);
			$this->success('删除成功！');
		}else {
			$this->error('删除失败！');
		}
	}
	
	//添加或更新
	function update($appid) {
		$model = D('Reply');
		$res = $model->update();
		if(!$res){
			$this->error($model->getError());
		}else{
			$this->success($res['id']?'更新成功':'新增成功', get_redirect_url());
		}
	}
}